#pragma once
#include <iostream>
#include <vector>

namespace xy {
	template<class T>
	class less {
	public:
		bool operator()(const T& a, const T& b) const {
			return a < b;
		}
	};

	template<class T>
	class greater {
	public:
		bool operator()(const T& a, const T& b) const {
			return a > b;
		}
	};

	template<class T, class container = vector<T>, class compare = less<T>>
	class priority_queue {
	private:
		void adjustDown(size_t parent) {
			compare com;
			size_t child = parent * 2 + 1;
			while (child < _con.size()) {
				if (child + 1 < _con.size() && com(_con[child], _con[child + 1])) {
					++child;
				}
				if (com(_con[parent], _con[child])) {
					std::swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else {
					break;
				}
			}
		}

		void adjustUp(size_t child) {
			compare com;
			size_t parent = (child - 1) / 2;
			while (child > 0) {
				if (com(_con[parent], _con[child])) {
					std::swap(_con[parent], _con[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else {
					break;
				}
			}
		}
	public:
		priority_queue() {}

		template<class inputIterator>	
		priority_queue(inputIterator first, inputIterator last) {
			while (first != last) {
				_con.push_back(*first++);
			}

			for (int i = (size() - 2) / 2; i >= 0; i--) {
				adjustDown(i);
			}
		}

		void push(const T& val) {
			_con.push_back(val);
			adjustUp(_con.size() - 1);
		}

		void pop() {
			std::swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjustDown(0);
		}

		const T& top() {
			return _con[0];
		}

		bool empty() {
			return _con.empty();
		}

		size_t size() const{
			return _con.size();
		}
	private:
		container _con;
	};
}